//
//  MYComposeToolBar.swift
//  NewMicroblog
//
//  Created by apple on 2016/11/1.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

enum MYComposeButtonType: Int {
       case picture = 0
        case mention = 1
    case trend = 2
    case emoticon = 3
    case add = 4
}

class MYComposeToolBar: UIStackView {
    
    ///执行相应的闭包
    var callBack:((MYComposeButtonType)->())?
    var emoticonButton: UIButton?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:- 搭建界面
    private func setupUI(){
        axis = .horizontal
        distribution = .fillEqually
                addChildButton(imageName: "compose_camerabutton_background",type: .picture)
                addChildButton(imageName: "compose_mentionbutton_background",type: .mention)
                addChildButton(imageName: "compose_trendbutton_background",type:  .trend)
             emoticonButton = addChildButton(imageName: "compose_emoticonbutton_background",type:  .emoticon)
                addChildButton(imageName: "compose_add_background",type:.add)
        
    }
    //添加子按钮
    @discardableResult
    private func addChildButton(imageName: String,type: MYComposeButtonType)->UIButton{
        let btn = UIButton(setImage: imageName, title: nil, tagart: self, action: #selector(buttonClick(btn:)))
        btn.tag = type.rawValue
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
        btn.adjustsImageWhenHighlighted = false
        addArrangedSubview(btn)
        return btn
    }
    
    /// 点击事件
    @objc func buttonClick(btn:UIButton){
        let type = MYComposeButtonType(rawValue: btn.tag)!
        callBack?(type)
    }
    
    func showIcon(isEmoticon: Bool){
        if isEmoticon {
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), for: .normal)
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), for: .highlighted)
        }else{
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
        }
    }
}
